package academic.emailClient.control.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import academic.emailClient.model.EmailClientModel;
import academic.emailClient.view.EmailClientViewInterface;
import academic.emailClient.view.entities.FolderView;

/**
 * Class responsible for resolve all  folder hierarchy functionalities
 * @author Alejandra Sanchez
 * @author Oualid El Halimi
 *
 */
public class FolderHierarchyActionListener implements ActionListener {
	
	private EmailClientViewInterface view;
	private EmailClientModel model;

	public FolderHierarchyActionListener(EmailClientViewInterface view,
										 EmailClientModel model) {
		super();
		this.view = view;
		this.model = model;
	}

	/**
	 * handle Folder Hierarchy Action event
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
    	String action = e.getActionCommand();
        if(action.equals("ADD")) {
        	addFolder();
        }        	
        else if(action.equals("REMOVE")){
        	removeFolder();
        }        		
	}
	
	/**
	 * update Sub folder Path List to add Folder
	 */
	private void addFolder(){
		FolderView newFolder = view.createSubfolder();
		model.createSubfolder(newFolder.getName(), newFolder.getParent());
		view.updateSubfolderPathList(model.getAllSubfoldersPaths());
		
	}
	
	/**
	 * update Sub folder Path List to remove Folder
	 */
	private void removeFolder(){
		String selectedFolderPath = view.getSelectedFolderPath();
		if (view.selectedFolderCanBeRemoved()){
			model.deleteSubFolder(selectedFolderPath);
			view.updateSubfolderPathList(model.getAllSubfoldersPaths());
		}
	}

}
